﻿using Sasc.PV.BLL;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sasc.PV.Web.Admin
{
    public partial class DingYi : PVAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定所有s的菜单项目
                BindTree(ListTreeView.Nodes, 0);

                //设置有权限的项才显示

                SetQuanXian();
            }
        }

        private void BindTree(TreeNodeCollection Nds, int IDStr)
        {
            var daohangIDList = DbHelperSQL.GetSHSL("select top 1 DaoHangIDList from ERPUser where UserName='" + PublicMethod.GetSessionValue("UserName") + "'");
            var MYDT = DbHelperSQL.GetDataSet("select * from ERPTreeList where ParentID=" + IDStr.ToString() + " order by PaiXuStr asc,ID asc");
            for (int i = 0; i < MYDT.Tables[0].Rows.Count; i++)
            {
                //if (MYDT.Tables[0].Rows[i]["NavigateUrlStr"].ToString().Trim().Length<=0||PublicMethod.StrIFIn("|" + MYDT.Tables[0].Rows[i]["ValueStr"].ToString() + "|", PublicMethod.GetSessionValue("QuanXian")) == true)
                //{
                TreeNode MenuNode = new TreeNode();
                MenuNode.Text = MYDT.Tables[0].Rows[i]["TextStr"].ToString();
                MenuNode.Value = MYDT.Tables[0].Rows[i]["ValueStr"].ToString();
                int strId = int.Parse(MYDT.Tables[0].Rows[i]["ID"].ToString());
                MenuNode.ImageUrl = MYDT.Tables[0].Rows[i]["ImageUrlStr"].ToString();

                if (MYDT.Tables[0].Rows[i]["NavigateUrlStr"].ToString().Trim().Length <= 0)
                {
                    MenuNode.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    MenuNode.SelectAction = TreeNodeSelectAction.Select;
                    MenuNode.NavigateUrl = MYDT.Tables[0].Rows[i]["NavigateUrlStr"].ToString();
                    MenuNode.Target = MYDT.Tables[0].Rows[i]["Target"].ToString();
                    if (daohangIDList.IndexOf(string.Format("|{0}|", MenuNode.Value)) > 0)
                        MenuNode.Checked = true;
                }
                Nds.Add(MenuNode);
                BindTree(Nds[Nds.Count - 1].ChildNodes, strId);
                //}            
            }
        }

        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        public void SetQuanXian()
        {
            if (PublicMethod.IsSuperAdmin())
                return;

            //判断权限分配
            for (int i = 0; i < this.ListTreeView.Nodes.Count; i++)
            {
                for (int j = 0; j < this.ListTreeView.Nodes[i].ChildNodes.Count; j++)
                {
                    //删除子菜单中的不在权限中的项
                    for (int k = 0; k < this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                    {
                        if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].Value + "|", PublicMethod.GetSessionValue("QuanXian")) == false)
                        {
                            this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k]);
                            k = -1;
                        }
                    }
                    //判断是父节点还是子节点
                    if (this.ListTreeView.Nodes[i].ChildNodes[j].SelectAction == TreeNodeSelectAction.Expand)
                    {
                        if (this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count <= 0)
                        {
                            this.ListTreeView.Nodes[i].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j]);
                            j = -1;
                        }
                    }
                    else
                    {
                        if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].ChildNodes[j].Value + "|", PublicMethod.GetSessionValue("QuanXian")) == false)
                        {
                            this.ListTreeView.Nodes[i].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j]);
                            j = -1;
                        }
                    }
                }
                //判断是父节点还是子节点
                if (this.ListTreeView.Nodes[i].SelectAction == TreeNodeSelectAction.Expand)
                {
                    if (this.ListTreeView.Nodes[i].ChildNodes.Count <= 0)
                    {
                        this.ListTreeView.Nodes.Remove(this.ListTreeView.Nodes[i]);
                        i = -1;
                    }
                }
                else
                {
                    if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].Value + "|", PublicMethod.GetSessionValue("QuanXian")) == false)
                    {
                        this.ListTreeView.Nodes.Remove(this.ListTreeView.Nodes[i]);
                        i = -1;
                    }
                }
            }
        }
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //将所有的前面的选择框中已经勾选的项，直接组合成一段html代码，存入数据库，Main.aspx页面直接显示这段html代码即可
            //&nbsp; &nbsp;<a class="bai" target="rform" href="../NetMail/NetMailShou.aspx"><img align="absMiddle" height="16" border=0 src="../images/TreeImages/webmail.gif" width="16" />&nbsp;Internet 邮件&nbsp;</a>

            var MyDaoHangIDList = "";
            var MyDaoHangList = "";
            for (int i = 0; i < this.ListTreeView.Nodes.Count; i++)
            {
                for (int j = 0; j < this.ListTreeView.Nodes[i].ChildNodes.Count; j++)
                {
                    var currentNode = this.ListTreeView.Nodes[i].ChildNodes[j];
                    if (currentNode.Checked == true)
                    {
                        //添加进入字符串
                        MyDaoHangList = MyDaoHangList + "&nbsp; &nbsp;<a class=\"bai\" target=\"rform\" href=\"" + WebUtils.MakeRelative( currentNode.NavigateUrl) + "\"><img align=\"absMiddle\" height=\"16\" border=0 src=\"" + currentNode.ImageUrl.Replace("~/images", "../images") + "\" width=\"16\" />&nbsp;" + currentNode.Text + "&nbsp;</a>";
                        MyDaoHangIDList += "|" + currentNode.Value;
                    }
                    else
                    {
                        for (int k = 0; k < currentNode.ChildNodes.Count; k++)
                        {
                            if (currentNode.ChildNodes[k].Checked == true)
                            {
                                //添加进入字符串
                                MyDaoHangList = MyDaoHangList + "&nbsp; &nbsp;<a class=\"bai\" target=\"rform\" href=\"" + WebUtils.MakeRelative(currentNode.NavigateUrl) + "\"><img align=\"absMiddle\" height=\"16\" border=0 src=\"" + currentNode.ChildNodes[k].ImageUrl.Replace("~/images", "../images") + "\" width=\"16\" />&nbsp;" + currentNode.ChildNodes[k].Text + "&nbsp;</a>";
                                MyDaoHangIDList += "|" + currentNode.Value;
                            }
                        }
                    }
                }
            }
            MyDaoHangIDList += "|";

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ERPUser set ");
            strSql.Append("DaoHangList=@DaoHangList");
            strSql.Append(",DaoHangIDList=@DaoHangIDList");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@DaoHangList", SqlDbType.NText),
                    new SqlParameter("@DaoHangIDList", SqlDbType.NVarChar, 4000)};
            parameters[0].Value = PublicMethod.GetSessionValue("UserName");
            parameters[1].Value = MyDaoHangList;
            parameters[2].Value = MyDaoHangIDList;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            Response.Write("<script language=javascript>alert('导航菜单配置完成！');top.location='Index';</script>");
        }

        #region Not Used Methods
        /// <summary>
        /// 检测传递进来的URLSTr，加入ValueStr参数
        /// </summary>
        /// <param name="NavStr"></param>
        /// <returns></returns>
        public string CheCKAndSetNavUrl(string NavStr, string ValueStr)
        {
            if (NavStr.Trim().Length > 0)
            {
                if (PublicMethod.StrIFIn("?", "NavStr") == true)
                {
                    return NavStr + "&ValueStr=" + ValueStr;
                }
                else
                {
                    return NavStr + "?ValueStr=" + ValueStr;
                }
            }
            else
            {
                return NavStr;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int PaiXuNum = 0;

            string TextStr = "";
            string ImageUrlStr = "";
            string ValueStr = "";
            string NavigateUrlStr = "";
            string Target = "rform";
            int ParentID = 0;
            string QuanXianList = "";
            string PaiXuStr = "";
            //开发时使用，将静态菜单写入数据库表中管理
            for (int i = 0; i < this.ListTreeView.Nodes.Count; i++)
            {
                //添加本节点进入数据库
                TextStr = this.ListTreeView.Nodes[i].Text.ToString();
                ImageUrlStr = this.ListTreeView.Nodes[i].ImageUrl.ToString();
                ValueStr = this.ListTreeView.Nodes[i].Value.ToString();
                NavigateUrlStr = this.ListTreeView.Nodes[i].NavigateUrl.ToString();
                ParentID = 0;
                if (NavigateUrlStr.Trim().Length > 0)
                {
                    QuanXianList = ValueStr + "A_添加|" + ValueStr + "M_修改|" + ValueStr + "D_删除|" + ValueStr + "E_导出";
                }
                PaiXuNum = PaiXuNum + 1;
                PaiXuStr = PaiXuNum.ToString();
                DbHelperSQL.ExecuteSQL("insert into ERPTreeList([TextStr], [ImageUrlStr], [ValueStr], [NavigateUrlStr], [Target], [ParentID], [QuanXianList], [PaiXuStr]) values ('" + TextStr + "','" + ImageUrlStr + "','" + ValueStr + "','" + NavigateUrlStr + "','" + Target + "'," + ParentID + ",'" + QuanXianList + "','" + PaiXuStr + "')");

                //循环此节点下的各个子节点
                for (int j = 0; j < this.ListTreeView.Nodes[i].ChildNodes.Count; j++)
                {
                    //添加本节点进入数据库
                    TextStr = this.ListTreeView.Nodes[i].ChildNodes[j].Text.ToString();
                    ImageUrlStr = this.ListTreeView.Nodes[i].ChildNodes[j].ImageUrl.ToString();
                    ValueStr = this.ListTreeView.Nodes[i].ChildNodes[j].Value.ToString();
                    NavigateUrlStr = this.ListTreeView.Nodes[i].ChildNodes[j].NavigateUrl.ToString();
                    ParentID = int.Parse(DbHelperSQL.GetSHSLInt("select top 1 ID from ERPTreeList where TextStr='" + this.ListTreeView.Nodes[i].Text.ToString() + "' order by ID desc"));
                    if (NavigateUrlStr.Trim().Length > 0)
                    {
                        QuanXianList = ValueStr + "A_添加|" + ValueStr + "M_修改|" + ValueStr + "D_删除|" + ValueStr + "E_导出";
                    }
                    PaiXuNum = PaiXuNum + 1;
                    PaiXuStr = PaiXuNum.ToString();
                    DbHelperSQL.ExecuteSQL("insert into ERPTreeList([TextStr], [ImageUrlStr], [ValueStr], [NavigateUrlStr], [Target], [ParentID], [QuanXianList], [PaiXuStr]) values ('" + TextStr + "','" + ImageUrlStr + "','" + ValueStr + "','" + NavigateUrlStr + "','" + Target + "'," + ParentID + ",'" + QuanXianList + "','" + PaiXuStr + "')");


                    for (int k = 0; k < this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                    {
                        //添加本节点进入数据库
                        TextStr = this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].Text.ToString();
                        ImageUrlStr = this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].ImageUrl.ToString();
                        ValueStr = this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].Value.ToString();
                        NavigateUrlStr = this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].NavigateUrl.ToString();
                        ParentID = int.Parse(DbHelperSQL.GetSHSLInt("select top 1 ID from ERPTreeList where TextStr='" + this.ListTreeView.Nodes[i].ChildNodes[j].Text.ToString() + "' order by ID desc"));
                        if (NavigateUrlStr.Trim().Length > 0)
                        {
                            QuanXianList = ValueStr + "A_添加|" + ValueStr + "M_修改|" + ValueStr + "D_删除|" + ValueStr + "E_导出";
                        }
                        PaiXuNum = PaiXuNum + 1;
                        PaiXuStr = PaiXuNum.ToString();
                        DbHelperSQL.ExecuteSQL("insert into ERPTreeList([TextStr], [ImageUrlStr], [ValueStr], [NavigateUrlStr], [Target], [ParentID], [QuanXianList], [PaiXuStr]) values ('" + TextStr + "','" + ImageUrlStr + "','" + ValueStr + "','" + NavigateUrlStr + "','" + Target + "'," + ParentID + ",'" + QuanXianList + "','" + PaiXuStr + "')");

                    }
                }
            }

            MessageBox.Show(this, "OK");
        }
        #endregion
    }
}